/*
  Copyright 2009 Smart Enterprise Solution Corp.
  Email: contact@ses.vn - Website: http://www.ses.vn
  KimNgan Project.
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Data.SqlClient;
using SES.CMS.DO;
/// <summary>
/// Summary description for cmsLoaiSanPhamDAL
/// </summary>
namespace SES.CMS.DAL
{

    public class cmsLoaiSanPhamDAL : BaseDAL
    {
        #region Private Variables

        #endregion

        #region Public Constructors
        public cmsLoaiSanPhamDAL()
        {
            //
            // TODO: Add constructor logic here
            //

        }
        #endregion



        #region Public Methods
        public int Insert(cmsLoaiSanPhamDO objcmsLoaiSanPhamDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsLoaiSanPham_Insert";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@TenLoaiSanPham", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsLoaiSanPhamDO.TenLoaiSanPham;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@MoTa", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsLoaiSanPhamDO.MoTa;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ParentID", SqlDbType.Int);
            Sqlparam.Value = objcmsLoaiSanPhamDO.ParentID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ID", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);


            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ID"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ID"].Value);

            return result;
        }

        public int Update(cmsLoaiSanPhamDO objcmsLoaiSanPhamDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsLoaiSanPham_UpdateByPK";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@LoaiSanPhamID", SqlDbType.Int);
            Sqlparam.Value = objcmsLoaiSanPhamDO.LoaiSanPhamID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@TenLoaiSanPham", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsLoaiSanPhamDO.TenLoaiSanPham;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@MoTa", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsLoaiSanPhamDO.MoTa;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ParentID", SqlDbType.Int);
            Sqlparam.Value = objcmsLoaiSanPhamDO.ParentID;
            Sqlcomm.Parameters.Add(Sqlparam);



            Sqlparam = new SqlParameter("@ErrorCode", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);

            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ErrorCode"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ErrorCode"].Value);

            return result;


        }

        public int Delete(cmsLoaiSanPhamDO objcmsLoaiSanPhamDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsLoaiSanPham_DeleteByPK";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@LoaiSanPhamID", SqlDbType.Int);
            Sqlparam.Value = objcmsLoaiSanPhamDO.LoaiSanPhamID;
            Sqlcomm.Parameters.Add(Sqlparam);



            int result = base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }

        public int DeleteAll()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsLoaiSanPham_DeleteAll";

            int result = base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }

        public cmsLoaiSanPhamDO Select(cmsLoaiSanPhamDO objcmsLoaiSanPhamDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsLoaiSanPham_GetByPK";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@LoaiSanPhamID", SqlDbType.Int);
            Sqlparam.Value = objcmsLoaiSanPhamDO.LoaiSanPhamID;
            Sqlcomm.Parameters.Add(Sqlparam);



            DataSet ds = base.GetDataSet(Sqlcomm);
            DataRow dr = null;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                dr = ds.Tables[0].Rows[0];
                if (!Convert.IsDBNull(dr["LoaiSanPhamID"]))
                    objcmsLoaiSanPhamDO.LoaiSanPhamID = Convert.ToInt32(dr["LoaiSanPhamID"]);
                if (!Convert.IsDBNull(dr["TenLoaiSanPham"]))
                    objcmsLoaiSanPhamDO.TenLoaiSanPham = Convert.ToString(dr["TenLoaiSanPham"]);
                if (!Convert.IsDBNull(dr["MoTa"]))
                    objcmsLoaiSanPhamDO.MoTa = Convert.ToString(dr["MoTa"]);
                if (!Convert.IsDBNull(dr["ParentID"]))
                    objcmsLoaiSanPhamDO.ParentID = Convert.ToInt32(dr["ParentID"]);

            }
            return objcmsLoaiSanPhamDO;
        }
        public cmsLoaiSanPhamDO SelectByTen(string ten)
        {
            cmsLoaiSanPhamDO objcmsLoaiSanPhamDO = new cmsLoaiSanPhamDO();
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsLoaiSanPham_getByTen";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@ten", SqlDbType.NVarChar);
            Sqlparam.Value = ten;
            Sqlcomm.Parameters.Add(Sqlparam);



            DataSet ds = base.GetDataSet(Sqlcomm);
            DataRow dr = null;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                dr = ds.Tables[0].Rows[0];
                if (!Convert.IsDBNull(dr["LoaiSanPhamID"]))
                    objcmsLoaiSanPhamDO.LoaiSanPhamID = Convert.ToInt32(dr["LoaiSanPhamID"]);
                if (!Convert.IsDBNull(dr["TenLoaiSanPham"]))
                    objcmsLoaiSanPhamDO.TenLoaiSanPham = Convert.ToString(dr["TenLoaiSanPham"]);
                if (!Convert.IsDBNull(dr["MoTa"]))
                    objcmsLoaiSanPhamDO.MoTa = Convert.ToString(dr["MoTa"]);
                if (!Convert.IsDBNull(dr["ParentID"]))
                    objcmsLoaiSanPhamDO.ParentID = Convert.ToInt32(dr["ParentID"]);

            }
            return objcmsLoaiSanPhamDO;
        }

        public ArrayList SelectAll1()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsLoaiSanPham_GetAll";

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;
            ArrayList arrcmsLoaiSanPhamDO = new ArrayList();
            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    cmsLoaiSanPhamDO objcmsLoaiSanPhamDO = new cmsLoaiSanPhamDO();
                    if (!Convert.IsDBNull(dr["LoaiSanPhamID"]))
                        objcmsLoaiSanPhamDO.LoaiSanPhamID = Convert.ToInt32(dr["LoaiSanPhamID"]);
                    if (!Convert.IsDBNull(dr["TenLoaiSanPham"]))
                        objcmsLoaiSanPhamDO.TenLoaiSanPham = Convert.ToString(dr["TenLoaiSanPham"]);
                    if (!Convert.IsDBNull(dr["MoTa"]))
                        objcmsLoaiSanPhamDO.MoTa = Convert.ToString(dr["MoTa"]);
                    if (!Convert.IsDBNull(dr["ParentID"]))
                        objcmsLoaiSanPhamDO.ParentID = Convert.ToInt32(dr["ParentID"]);
                    arrcmsLoaiSanPhamDO.Add(objcmsLoaiSanPhamDO);
                }
            }
            return arrcmsLoaiSanPhamDO;
        }

        public DataTable SelectAll(int id)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsLoaiSanPham_GetAll";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@id", SqlDbType.Int);
            Sqlparam.Value = id;
            Sqlcomm.Parameters.Add(Sqlparam);

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;
        }

        public DataTable SelectLoaiSanPham_ToMenu(int from, int to)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsLoaiSanPham_SelectLoaiSP";

            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@from",SqlDbType.Int);
            Sqlparam.Value = from;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@to",SqlDbType.Int);
            Sqlparam.Value = to;
            Sqlcomm.Parameters.Add(Sqlparam);

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }
            return dt;
        }


        #endregion

    }

}
